/*
 * @Author: qsy
 * @Date: 2024-07-23 14:44:15
 * @LastEditors: qsy
 * @LastEditTime: 2025-04-06 14:12:37
 * @Description: 请填写简介
 */
#include "my_mqtt.h"

static const char *TAG = "MQTTS_EXAMPLE";

bool init_data_flag = false; // 初始化数据标志位




static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
    ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); // 显示事件
    esp_mqtt_event_handle_t event = event_data; // 获取事件数据
    esp_mqtt_client_handle_t client = event->client; // 获取MQTT客户端句柄
    char topic[128];
    int msg_id;
    switch ((esp_mqtt_event_id_t)event_id) {
    case MQTT_EVENT_CONNECTED: // 连接成功
        ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
        sprintf(topic, MQTT_SUB_TOPIC, DEVICE_ID);
        msg_id = esp_mqtt_client_subscribe(client, topic, 1);
        if(msg_id != -1){
            ESP_LOGI(TAG, "subscribe topic %s, msg_id=%d", topic, msg_id);
        }
        // 发送测试数据
        char send_topic[128];
        sprintf(send_topic, MQTT_PUB_TOPIC, DEVICE_ID);
        char my_test_data[] = {"hello"};
        int ret = esp_mqtt_client_publish(client, send_topic, my_test_data, sizeof(my_test_data), 1, 0);
        if(ret != -1){
            ESP_LOGI(TAG, "publish topic %s, msg_id=%d", send_topic, ret);
        }

        break;
    case MQTT_EVENT_DISCONNECTED: // 连接断开
        ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");

        break;

    case MQTT_EVENT_SUBSCRIBED: // 订阅成功
        ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
        break;
    case MQTT_EVENT_UNSUBSCRIBED: // 取消订阅成功
        ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED");
        break;
    case MQTT_EVENT_PUBLISHED: // 发布成功
        ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED");
        break;
    case MQTT_EVENT_DATA: // 收到消息
        ESP_LOGI(TAG, "MQTT_EVENT_DATA");
        printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); // 这个是打印主题名字
        printf("DATA=%.*s\r\n", event->data_len, event->data); // 这是打印收到的数据

        break;
    //-----------------------收到消息操作--------------------

    case MQTT_EVENT_ERROR: // 错误

        break;
    default:
        // ESP_LOGI(TAG, "Other event id:%d", event->event_id);
        // _ui_flag_modify(ui_wifi_warning, LV_OBJ_FLAG_HIDDEN, _UI_MODIFY_FLAG_REMOVE);
        break;
    }
}




esp_mqtt_client_handle_t client;
void mqtt_app_start(void)
{

    // 客户id
    char my_client_id[] = {"pillow"};

    // MQTT 服务器地址
    const esp_mqtt_client_config_t mqtt_cfg = {

        .uri = URL_TEST, // MQTT服务器地址
        .port = MQTT_PORT, // 端口
        .client_id = my_client_id, // 客户端ID

        // 设置遗嘱消息
        .lwt_qos = 1,                       // 遗嘱消息的 QoS
        .lwt_retain = 0,                    // 遗嘱消息是否需要保留
        // .disable_auto_reconnect = false,        // 禁用自动重连
        .out_buffer_size = 1024,            // 减小输出缓冲区大小
        .buffer_size = 1024,                // 减小输入缓冲区大小
        .task_stack = 1024*4,                 // 调整任务堆栈大小
        .network_timeout_ms = 3000,         // 网络超时时间
    };

    // 初始化MQTT客户端
    client = esp_mqtt_client_init(&mqtt_cfg);
    if (client == NULL) {
        ESP_LOGE(TAG, "Failed to initialize MQTT client");
        return;
    }

    // 注册事件处理函数
    esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);

    // 启动MQTT客户端
    esp_err_t err = esp_mqtt_client_start(client);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Failed to start MQTT client: %d", err);
    }

}



/**
 * @description: 关闭mqtt
 * @return {*}
 */
void mqtt_app_stop(void){

    // 停止MQTT客户端
    esp_mqtt_client_stop(client);
    // 断开 MQTT 连接
    esp_mqtt_client_disconnect(client);
    // 销毁MQTT客户端
    esp_mqtt_client_destroy(client); 
}